
# Load packages
  library(haven)
  library(broom)
  library(lmtest)
  library(sandwich)
  library(stargazer)
  library(tidyverse)
  
# Calculate robust confidence intervals
  se_robust <- function(x)
    coeftest(x, vcov = vcovHC(x, type = "HC0"))[, "Std. Error"]
  p_robust <- function(x)
    coeftest(x, vcov = vcovHC(x, type = "HC0"))[, "Pr(>|t|)"]
  
  #### GLES Querschnitt 2009-2017, Kumulation
  
  ##########
  ## 2009 ##
  ##########
  
  data <- read_dta("ObsStudy_00_data_ObsStudy_01_05_Germany_GLES_pre-election_cross-section_2009_2013.dta")
  
  # Select 2009 pre-election data
  dt09 <- data[data$sample == 1, ]
  
  # Set values smaller 0 to NA as those values simply categorize reasons for missing values
  dt09[dt09 < 0] <- NA
  
  # Evaluation political parties
  colnames(dt09)[seq(132, 138, 1)] <- c("rat_CDU", "rat_CSU", "rat_SPD", "rat_FDP", "rat_GRUENE", "rat_LINKE", "rat_AfD")
  
  # Coalition evaluation
  colnames(dt09)[seq(143, 150, 1)] <- c("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE", "rat_coal_SPD_FDP", "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE", "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE")
  
  # Coalition likelihood
  colnames(dt09)[seq(151, 156, 1)] <- c("coallik_CDU_CSU_SPD", "coallik_CDU_CSU_FDP", "coallik_SPD_GRUENE", "coallik_SPD_FDP_GRUENE", "coallik_CDU_CSU_FDP_GRUENE", "coallik_SPD_GRUENE_LINKE")
  
  # Generate party choice variable
  dt09$vote <- dt09$v9bb
  dt09$vote <- ifelse(is.na(dt09$vote), dt09$v14bb, dt09$vote)

  # Assign interpretable labels to vote variable
  dt09$vote[dt09$vote==1] <- "UNION"
  dt09$vote[dt09$vote==4] <- "SPD"
  dt09$vote[dt09$vote==5] <- "FDP"
  dt09$vote[dt09$vote==6] <- "GRUENE"
  dt09$vote[dt09$vote==7] <- "LINKE"
  
  # Generate ptv columns based on vote variable
  dt09$ptv_UNION <- ifelse(dt09$vote == "UNION", 1, 0)
  dt09$ptv_SPD <- ifelse(dt09$vote == "SPD", 1, 0)
  dt09$ptv_FDP <- ifelse(dt09$vote == "FDP", 1, 0)
  dt09$ptv_GRUENE <- ifelse(dt09$vote == "GRUENE", 1, 0)
  dt09$ptv_LINKE <- ifelse(dt09$vote == "LINKE", 1, 0)
  
  Z <- dt09 %>% select("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE", "rat_coal_SPD_FDP_GRUENE", "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE") %>%
    mutate_all(as.numeric) %>%
    mutate_all(funs(scales::rescale(.,to = c(0, 1))))
  
  ##### CDU/CSU #####
  # Coalition evaluation
  rat_coal_CDU_CSU <- Z %>% select(-c(rat_coal_SPD_GRUENE, rat_coal_SPD_FDP_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_CDU_CSU <- dt09 %>% select(coallik_CDU_CSU_SPD, coallik_CDU_CSU_FDP, coallik_CDU_CSU_FDP_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_CDU_CSU_FDP) + exp(coallik_CDU_CSU_FDP_GRUENE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### SPD #####
  # Coalition evaluation
  rat_coal_SPD <- Z %>% select(-c(rat_coal_CDU_CSU_FDP, rat_coal_CDU_CSU_FDP_GRUENE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_SPD <- dt09 %>% select(coallik_CDU_CSU_SPD, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### FDP #####
  # Coalition evaluation
  rat_coal_FDP <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_SPD_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_FDP <- dt09 %>% select(coallik_CDU_CSU_FDP, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_FDP_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_FDP) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_FDP_GRUENE),
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### GRUENE #####
  # Coalition evaluation
  rat_coal_GRUENE <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_CDU_CSU_FDP)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood
  coal_lik_GRUENE <- dt09 %>% select(coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_FDP_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_FDP_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_FDP_GRUENE = exp(coallik_CDU_CSU_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### LINKE #####
  # Coalition evaluation
  rat_coal_LINKE <- Z %>% select(rat_coal_SPD_GRUENE_LINKE) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition likelihood, has to be 1 as only one coalition option available
  coal_lik_LINKE <- dt09 %>% select(coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_SPD_GRUENE_LINKE),
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  # Mean-Variance Model
  EV <- function(gamma,Z){
    gamma %*% Z
  }
  
  Vari <- function(gamma,Z){
    gamma %*% ((Z - as.numeric(EV(gamma,Z)))^2)
  }
  
  
  N <- nrow(coal_lik_CDU_CSU)
  M_CDU_CSU <- sapply(1:N, function(i) EV(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  V_CDU_CSU <- sapply(1:N, function(i) Vari(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  
  N <- nrow(coal_lik_SPD)
  M_SPD <- sapply(1:N, function(i) EV(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  V_SPD <- sapply(1:N, function(i) Vari(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  
  N <- nrow(coal_lik_FDP)
  M_FDP <- sapply(1:N, function(i) EV(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  V_FDP <- sapply(1:N, function(i) Vari(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  
  N <- nrow(coal_lik_GRUENE)
  M_GRUENE <- sapply(1:N, function(i) EV(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  V_GRUENE <- sapply(1:N, function(i) Vari(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  
  N <- nrow(coal_lik_LINKE)
  M_LINKE <- sapply(1:N, function(i) EV(coal_lik_LINKE[i,], rat_coal_LINKE[i,]))
  V_LINKE <- sapply(1:N, function(i) Vari(coal_lik_LINKE[i,], rat_coal_LINKE[i,])) # is 0

  # Generate a gender dummy "male"
  dt09$male <- ifelse(dt09$d1 == 2, 0, dt09$d1)
  
  # PID
  if (!exists("robustnesscheck_pid")) {
    robustnesscheck_pid <- FALSE # PID as robustness?
  }
  dt09$pid_UNION <- ifelse(dt09$v25a==1|dt09$v25a==2|dt09$v25a==3, 1, 0)
  dt09$pid_SPD <- ifelse(dt09$v25a==4, 1, 0)
  dt09$pid_FDP <- ifelse(dt09$v25a==5, 1, 0)
  dt09$pid_GRUENE <- ifelse(dt09$v25a==6, 1, 0)
  dt09$pid_LINKE <- ifelse(dt09$v25a==7, 1, 0)

  d_gles_cs_2009 <- data.frame(
    "ptv" = dt09$vote,
    "ptv_UNION" = dt09$ptv_UNION,
    "ptv_SPD" = dt09$ptv_SPD,
    "ptv_FDP" = dt09$ptv_FDP,
    "ptv_GRUENE" = dt09$ptv_GRUENE,
    "ptv_LINKE" = dt09$ptv_LINKE,
    "rat.UNION" = (dt09$rat_CDU + dt09$rat_CSU)/2,
    "rat.SPD" = dt09$rat_SPD,
    "rat.FDP" = dt09$rat_FDP,
    "rat.GRUENE" = dt09$rat_GRUENE,
    "rat.LINKE" = dt09$rat_LINKE,
    "pid_UNION" = dt09$pid_UNION,
    "pid_SPD" = dt09$pid_SPD,
    "pid_FDP" = dt09$pid_FDP,
    "pid_GRUENE" = dt09$pid_GRUENE,
    "pid_LINKE" = dt09$pid_LINKE,
    "lotterymean.UNION" = M_CDU_CSU,
    "lotteryvariance.UNION" = V_CDU_CSU,
    "lotterymean.SPD" = M_SPD,
    "lotteryvariance.SPD" = V_SPD,
    "lotterymean.FDP" = M_FDP,
    "lotteryvariance.FDP" = V_FDP,
    "lotterymean.GRUENE" = M_GRUENE,
    "lotteryvariance.GRUENE" = V_GRUENE,
    "lotterymean.LINKE" = M_LINKE,
    "lotteryvariance.LINKE" = V_LINKE,
    "sex" = dt09$male,
    "age" = dt09$d2,
    "edu" = dt09$d5 # Bildung:Schule
  )
  
  d_gles_cs_2009$lotteryvariance.UNION <- scales::rescale(d_gles_cs_2009$lotteryvariance.UNION, to = c(0, 1), from = c(0,0.25))
  d_gles_cs_2009$lotteryvariance.SPD <- scales::rescale(d_gles_cs_2009$lotteryvariance.SPD, to = c(0, 1), from = c(0,0.25))
  d_gles_cs_2009$lotteryvariance.FDP <- scales::rescale(d_gles_cs_2009$lotteryvariance.FDP, to = c(0, 1), from = c(0,0.25))
  d_gles_cs_2009$lotteryvariance.GRUENE <- scales::rescale(d_gles_cs_2009$lotteryvariance.GRUENE, to = c(0, 1), from = c(0,0.25))
  d_gles_cs_2009$lotteryvariance.LINKE <- scales::rescale(d_gles_cs_2009$lotteryvariance.LINKE, to = c(0, 1), from = c(0,0.25))
  
# Save model results
  
  summary(m1.gles.cs.2009 <- lm(as.formula(paste("ptv_UNION ~ lotteryvariance.UNION + lotterymean.UNION + rat.UNION  + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_UNION" else "")), d_gles_cs_2009))
  summary(m2.gles.cs.2009 <- lm(as.formula(paste("ptv_SPD ~ lotteryvariance.SPD + lotterymean.SPD + rat.SPD + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_SPD" else "")), d_gles_cs_2009))
  summary(m3.gles.cs.2009 <- lm(as.formula(paste("ptv_FDP ~ lotteryvariance.FDP + lotterymean.FDP + rat.FDP + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_FDP" else "")), d_gles_cs_2009))
  summary(m4.gles.cs.2009 <- lm(as.formula(paste("ptv_GRUENE ~ lotteryvariance.GRUENE + lotterymean.GRUENE + rat.GRUENE + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_GRUENE" else "")), d_gles_cs_2009))
  summary(m5.gles.cs.2009 <- lm(as.formula(paste("ptv_LINKE ~ lotteryvariance.LINKE + lotterymean.LINKE + rat.LINKE + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_LINKE" else "")), d_gles_cs_2009))
  
  GER_2009_PECS_UNION_Variance_Estimate <- tidy(m1.gles.cs.2009) %>% filter(term=="lotteryvariance.UNION") %>% select("estimate")
  GER_2009_PECS_UNION_Variance_SE <- se_robust(m1.gles.cs.2009)["lotteryvariance.UNION"] #tidy(m1.gles.cs.2009) %>% filter(term=="lotteryvariance.UNION") %>% select("std.error")
  GER_2009_PECS_UNION_Mean_Estimate <- tidy(m1.gles.cs.2009) %>% filter(term=="lotterymean.UNION") %>% select("estimate")
  GER_2009_PECS_UNION_Mean_SE <- se_robust(m1.gles.cs.2009)["lotterymean.UNION"] #tidy(m1.gles.cs.2009) %>% filter(term=="lotterymean.UNION") %>% select("std.error")
  
  GER_2009_PECS_SPD_Variance_Estimate <- tidy(m2.gles.cs.2009) %>% filter(term=="lotteryvariance.SPD") %>% select("estimate")
  GER_2009_PECS_SPD_Variance_SE <- se_robust(m2.gles.cs.2009)["lotteryvariance.SPD"] #tidy(m2.gles.cs.2009) %>% filter(term=="lotteryvariance.SPD") %>% select("std.error")
  GER_2009_PECS_SPD_Mean_Estimate <- tidy(m2.gles.cs.2009) %>% filter(term=="lotterymean.SPD") %>% select("estimate")
  GER_2009_PECS_SPD_Mean_SE <- se_robust(m2.gles.cs.2009)["lotterymean.SPD"] #tidy(m2.gles.cs.2009) %>% filter(term=="lotterymean.SPD") %>% select("std.error")
  
  GER_2009_PECS_FDP_Variance_Estimate <- tidy(m3.gles.cs.2009) %>% filter(term=="lotteryvariance.FDP") %>% select("estimate")
  GER_2009_PECS_FDP_Variance_SE <- se_robust(m3.gles.cs.2009)["lotteryvariance.FDP"] #tidy(m3.gles.cs.2009) %>% filter(term=="lotteryvariance.FDP") %>% select("std.error")
  GER_2009_PECS_FDP_Mean_Estimate <- tidy(m3.gles.cs.2009) %>% filter(term=="lotterymean.FDP") %>% select("estimate")
  GER_2009_PECS_FDP_Mean_SE <- se_robust(m3.gles.cs.2009)["lotterymean.FDP"] #tidy(m3.gles.cs.2009) %>% filter(term=="lotterymean.FDP") %>% select("std.error")
  
  GER_2009_PECS_GR_Variance_Estimate <- tidy(m4.gles.cs.2009) %>% filter(term=="lotteryvariance.GRUENE") %>% select("estimate")
  GER_2009_PECS_GR_Variance_SE <- se_robust(m4.gles.cs.2009)["lotteryvariance.GRUENE"] #tidy(m4.gles.cs.2009) %>% filter(term=="lotteryvariance.GRUENE") %>% select("std.error")
  GER_2009_PECS_GR_Mean_Estimate <- tidy(m4.gles.cs.2009) %>% filter(term=="lotterymean.GRUENE") %>% select("estimate")
  GER_2009_PECS_GR_Mean_SE <- se_robust(m4.gles.cs.2009)["lotterymean.GRUENE"] #tidy(m4.gles.cs.2009) %>% filter(term=="lotterymean.GRUENE") %>% select("std.error")

  # Harmonize names of the IVs of interest in the models
  names(m1.gles.cs.2009$coefficients)[names(m1.gles.cs.2009$coefficients) == "lotteryvariance.UNION"] <- "Government Lottery Variance"
  names(m1.gles.cs.2009$coefficients)[names(m1.gles.cs.2009$coefficients) == "lotterymean.UNION"] <- "Government Lottery Mean"
  names(m2.gles.cs.2009$coefficients)[names(m2.gles.cs.2009$coefficients) == "lotteryvariance.SPD"] <- "Government Lottery Variance"
  names(m2.gles.cs.2009$coefficients)[names(m2.gles.cs.2009$coefficients) == "lotterymean.SPD"] <- "Government Lottery Mean"
  names(m3.gles.cs.2009$coefficients)[names(m3.gles.cs.2009$coefficients) == "lotteryvariance.FDP"] <- "Government Lottery Variance"
  names(m3.gles.cs.2009$coefficients)[names(m3.gles.cs.2009$coefficients) == "lotterymean.FDP"] <- "Government Lottery Mean"
  names(m4.gles.cs.2009$coefficients)[names(m4.gles.cs.2009$coefficients) == "lotteryvariance.GRUENE"] <- "Government Lottery Variance"
  names(m4.gles.cs.2009$coefficients)[names(m4.gles.cs.2009$coefficients) == "lotterymean.GRUENE"] <- "Government Lottery Mean"
  names(m5.gles.cs.2009$coefficients)[names(m5.gles.cs.2009$coefficients) == "lotteryvariance.LINKE"] <- "Government Lottery Variance"
  names(m5.gles.cs.2009$coefficients)[names(m5.gles.cs.2009$coefficients) == "lotterymean.LINKE"] <- "Government Lottery Mean"
  
  if(!robustnesscheck_pid){
  stargazer(m1.gles.cs.2009, m2.gles.cs.2009, m3.gles.cs.2009, m4.gles.cs.2009, title="Linear regressions of vote choice on perceived government lottery variance and mean (GLES Cross Section (Pre-Election) 2009).", 
             dep.var.labels=c("Union", "SPD", "FDP", "Greens", "Left"), no.space=T,
            omit.stat=c("LL","ser","f"),
            se = lapply(list(m1.gles.cs.2009, m2.gles.cs.2009, m3.gles.cs.2009, m4.gles.cs.2009), se_robust),
            p = lapply(list(m1.gles.cs.2009, m2.gles.cs.2009, m3.gles.cs.2009, m4.gles.cs.2009), p_robust),
            type = "text",
            out = "TableSM10.tex"
            )
  }
  
  
  
  
  ##########
  ## 2013 ##
  ##########
  
  #### GLES Cross-Section Pre-election 2013
  data <- read_dta("ObsStudy_00_data_ObsStudy_01_05_Germany_GLES_pre-election_cross-section_2009_2013.dta")
  
  # Select 2013 pre-election data
  dt13 <- data[data$sample == 3, ]
  
  # Set values smaller 0 to NA as those values simply categorize reasons for missing values
  dt13[dt13 < 0] <- NA
  
  # Evaluation political parties
  colnames(dt13)[seq(132, 138, 1)] <- c("rat_CDU", "rat_CSU", "rat_SPD", "rat_FDP", "rat_GRUENE", "rat_LINKE", "rat_AfD")
  
  # Coalition evaluation
  colnames(dt13)[seq(143, 150, 1)] <- c("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE", "rat_coal_SPD_FDP", "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE", "rat_coal_CDU_CSU_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE")
  
  # Coalition likelihood
  colnames(dt13)[seq(160, 165, 1)] <- c("coallik_CDU_CSU_FDP", "coallik_SPD_GRUENE", "coallik_CDU_CSU_SPD", "coallik_CDU_CSU_GRUENE", "coallik_SPD_FDP_GRUENE", "coallik_SPD_GRUENE_LINKE")
  
  # Dependent variable
  dt13$vote <- dt13$v9bb
  dt13$vote <- ifelse(is.na(dt13$vote), dt13$v14bb, dt13$vote)

  # Assign interpretable labels to vote variable
  dt13$vote[dt13$vote==1] <- "UNION"
  dt13$vote[dt13$vote==4] <- "SPD"
  dt13$vote[dt13$vote==5] <- "FDP"
  dt13$vote[dt13$vote==6] <- "GRUENE"
  dt13$vote[dt13$vote==7] <- "LINKE"
  
  ## Generate ptv columns based on vote variable
  dt13$ptv_UNION <- ifelse(dt13$vote == "UNION", 1, 0)
  dt13$ptv_SPD <- ifelse(dt13$vote == "SPD", 1, 0)
  dt13$ptv_FDP <- ifelse(dt13$vote == "FDP", 1, 0)
  dt13$ptv_GRUENE <- ifelse(dt13$vote == "GRUENE", 1, 0)
  dt13$ptv_LINKE <- ifelse(dt13$vote == "LINKE", 1, 0)
  
  Z <- dt13 %>% select("rat_coal_CDU_CSU_SPD", "rat_coal_CDU_CSU_FDP", "rat_coal_SPD_GRUENE", "rat_coal_CDU_CSU_GRUENE", "rat_coal_SPD_FDP_GRUENE", "rat_coal_SPD_GRUENE_LINKE") %>%
    mutate_all(as.numeric) %>%
    mutate_all(funs(scales::rescale(.,to = c(0, 1)))) 
  
  ##### CDU/CSU #####
  # Coalition Evaluation
  rat_coal_CDU_CSU <- Z %>% select(-c(rat_coal_SPD_GRUENE, rat_coal_SPD_FDP_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition Liklihood
  coal_lik_CDU_CSU <- dt13 %>% select(coallik_CDU_CSU_SPD, coallik_CDU_CSU_FDP, coallik_CDU_CSU_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_CDU_CSU_FDP) + exp(coallik_CDU_CSU_GRUENE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### SPD #####
  # Coalition Evaluation
  rat_coal_SPD <- Z %>% select(-c(rat_coal_CDU_CSU_FDP, rat_coal_CDU_CSU_GRUENE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition Liklihood
  coal_lik_SPD <- dt13 %>% select(coallik_CDU_CSU_SPD, coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_SPD) + exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_CDU_CSU_SPD = exp(coallik_CDU_CSU_SPD)/sum_exp,
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### FDP #####
  # Coalition Evaluation
  rat_coal_FDP <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_SPD_GRUENE, rat_coal_CDU_CSU_GRUENE, rat_coal_SPD_GRUENE_LINKE)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition Liklihood
  coal_lik_FDP <- dt13 %>% select(coallik_CDU_CSU_FDP, coallik_SPD_FDP_GRUENE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_CDU_CSU_FDP) + exp(coallik_SPD_FDP_GRUENE),
           coallik_CDU_CSU_FDP = exp(coallik_CDU_CSU_FDP)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### GRUENE #####
  # Coalition Evaluation
  rat_coal_GRUENE <- Z %>% select(-c(rat_coal_CDU_CSU_SPD, rat_coal_CDU_CSU_FDP)) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition Liklihood
  coal_lik_GRUENE <- dt13 %>% select(coallik_SPD_GRUENE, coallik_SPD_FDP_GRUENE, coallik_CDU_CSU_GRUENE, coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp = exp(coallik_SPD_GRUENE) + exp(coallik_SPD_FDP_GRUENE) + exp(coallik_CDU_CSU_GRUENE) + exp(coallik_SPD_GRUENE_LINKE),
           coallik_SPD_GRUENE = exp(coallik_SPD_GRUENE)/sum_exp,
           coallik_SPD_FDP_GRUENE = exp(coallik_SPD_FDP_GRUENE)/sum_exp,
           coallik_CDU_CSU_GRUENE = exp(coallik_CDU_CSU_GRUENE)/sum_exp,
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  ##### LINKE #####
  # Coalition Evaluation
  rat_coal_LINKE <- Z %>% select(rat_coal_SPD_GRUENE_LINKE) %>%
    mutate_all(as.numeric) %>%
    as.matrix()
  
  # Coalition Likelihood, has to be 1 as only one coalition option available
  coal_lik_LINKE <- dt13 %>% select(coallik_SPD_GRUENE_LINKE)  %>%
    mutate_all(as.numeric) %>%
    mutate(sum_exp =  exp(coallik_SPD_GRUENE_LINKE),
           coallik_SPD_GRUENE_LINKE = exp(coallik_SPD_GRUENE_LINKE)/sum_exp) %>%
    select(-sum_exp) %>%
    as.matrix()
  
  # Mean-Variance Model
  EV <- function(gamma,Z){
    gamma %*% Z
  }
  
  Vari <- function(gamma,Z){
    gamma %*% ((Z - as.numeric(EV(gamma,Z)))^2)
  }
  
  N <- nrow(coal_lik_CDU_CSU)
  M_CDU_CSU <- sapply(1:N, function(i) EV(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  V_CDU_CSU <- sapply(1:N, function(i) Vari(coal_lik_CDU_CSU[i,], rat_coal_CDU_CSU[i,]))
  
  N <- nrow(coal_lik_SPD)
  M_SPD <- sapply(1:N, function(i) EV(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  V_SPD <- sapply(1:N, function(i) Vari(coal_lik_SPD[i,], rat_coal_SPD[i,]))
  
  N <- nrow(coal_lik_FDP)
  M_FDP <- sapply(1:N, function(i) EV(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  V_FDP <- sapply(1:N, function(i) Vari(coal_lik_FDP[i,], rat_coal_FDP[i,]))
  
  N <- nrow(coal_lik_GRUENE)
  M_GRUENE <- sapply(1:N, function(i) EV(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  V_GRUENE <- sapply(1:N, function(i) Vari(coal_lik_GRUENE[i,], rat_coal_GRUENE[i,]))
  
  N <- nrow(coal_lik_LINKE)
  M_LINKE <- sapply(1:N, function(i) EV(coal_lik_LINKE[i,], rat_coal_LINKE[i,]))
  V_LINKE <- sapply(1:N, function(i) Vari(coal_lik_LINKE[i,], rat_coal_LINKE[i,])) # is 0
  
  # Generate a gender dummy "male"
  dt13$male <- ifelse(dt13$d1 == 2, 0, dt13$d1)
  
  # PID
  if (!exists("robustnesscheck_pid")) {
    robustnesscheck_pid <- FALSE # PID as robustness?
  }
  dt13$pid_UNION <- ifelse(dt13$v25a==1|dt13$v25a==2|dt13$v25a==3, 1, 0)
  dt13$pid_SPD <- ifelse(dt13$v25a==4, 1, 0)
  dt13$pid_FDP <- ifelse(dt13$v25a==5, 1, 0)
  dt13$pid_GRUENE <- ifelse(dt13$v25a==6, 1, 0)
  dt13$pid_LINKE <- ifelse(dt13$v25a==7, 1, 0)

  # Age
  d_gles_cs_2013 <- data.frame(
    "ptv" = dt13$vote,
    "ptv_UNION" = dt13$ptv_UNION,
    "ptv_SPD" = dt13$ptv_SPD,
    "ptv_FDP" = dt13$ptv_FDP,
    "ptv_GRUENE" = dt13$ptv_GRUENE,
    "ptv_LINKE" = dt13$ptv_LINKE,
    "rat.UNION" = (dt13$rat_CDU + dt13$rat_CSU)/2,
    "rat.SPD" = dt13$rat_SPD,
    "rat.FDP" = dt13$rat_FDP,
    "rat.GRUENE" = dt13$rat_GRUENE,
    "rat.LINKE" = dt13$rat_LINKE,
    "pid_UNION" = dt13$pid_UNION,
    "pid_SPD" = dt13$pid_SPD,
    "pid_FDP" = dt13$pid_FDP,
    "pid_GRUENE" = dt13$pid_GRUENE,
    "pid_LINKE" = dt13$pid_LINKE,
    "lotterymean.UNION" = M_CDU_CSU,
    "lotteryvariance.UNION" = V_CDU_CSU,
    "lotterymean.SPD" = M_SPD,
    "lotteryvariance.SPD" = V_SPD,
    "lotterymean.FDP" = M_FDP,
    "lotteryvariance.FDP" = V_FDP,
    "lotterymean.GRUENE" = M_GRUENE,
    "lotteryvariance.GRUENE" = V_GRUENE,
    "lotterymean.LINKE" = M_LINKE,
    "lotteryvariance.LINKE" = V_LINKE,
    "sex" = dt13$male,
    "age" = dt13$d2,
    "edu" = dt13$d5 # Bildung:Schule
  )
  
  d_gles_cs_2013$lotteryvariance.UNION <- scales::rescale(d_gles_cs_2013$lotteryvariance.UNION, to = c(0, 1), from = c(0,0.25))
  d_gles_cs_2013$lotteryvariance.SPD <- scales::rescale(d_gles_cs_2013$lotteryvariance.SPD, to = c(0, 1), from = c(0,0.25))
  d_gles_cs_2013$lotteryvariance.FDP <- scales::rescale(d_gles_cs_2013$lotteryvariance.FDP, to = c(0, 1), from = c(0,0.25))
  d_gles_cs_2013$lotteryvariance.GRUENE <- scales::rescale(d_gles_cs_2013$lotteryvariance.GRUENE, to = c(0, 1), from = c(0,0.25))
  d_gles_cs_2013$lotteryvariance.LINKE <- scales::rescale(d_gles_cs_2013$lotteryvariance.LINKE, to = c(0, 1), from = c(0,0.25))
  
# Save model results
  summary(m1.gles.cs.2013 <- lm(as.formula(paste("ptv_UNION ~ lotteryvariance.UNION + lotterymean.UNION + rat.UNION  + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_UNION" else "")), d_gles_cs_2013))
  summary(m2.gles.cs.2013 <- lm(as.formula(paste("ptv_SPD ~ lotteryvariance.SPD + lotterymean.SPD + rat.SPD + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_SPD" else "")), d_gles_cs_2013))
  summary(m3.gles.cs.2013 <- lm(as.formula(paste("ptv_FDP ~ lotteryvariance.FDP + lotterymean.FDP + rat.FDP + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_FDP" else "")), d_gles_cs_2013))
  summary(m4.gles.cs.2013 <- lm(as.formula(paste("ptv_GRUENE ~ lotteryvariance.GRUENE + lotterymean.GRUENE + rat.GRUENE + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_GRUENE" else "")), d_gles_cs_2013))
  summary(m5.gles.cs.2013 <- lm(as.formula(paste("ptv_LINKE ~ lotteryvariance.LINKE + lotterymean.LINKE + rat.LINKE + sex + as.factor(edu) + age", 
                                                 if (robustnesscheck_pid) "+ pid_LINKE" else "")), d_gles_cs_2013))
  
  GER_2013_PECS_UNION_Variance_Estimate <- tidy(m1.gles.cs.2013) %>% filter(term=="lotteryvariance.UNION") %>% select("estimate")
  GER_2013_PECS_UNION_Variance_SE <- se_robust(m1.gles.cs.2013)["lotteryvariance.UNION"] #tidy(m1.gles.cs.2013) %>% filter(term=="lotteryvariance.UNION") %>% select("std.error")
  GER_2013_PECS_UNION_Mean_Estimate <- tidy(m1.gles.cs.2013) %>% filter(term=="lotterymean.UNION") %>% select("estimate")
  GER_2013_PECS_UNION_Mean_SE <- se_robust(m1.gles.cs.2013)["lotterymean.UNION"] #tidy(m1.gles.cs.2013) %>% filter(term=="lotterymean.UNION") %>% select("std.error")
  
  GER_2013_PECS_SPD_Variance_Estimate <- tidy(m2.gles.cs.2013) %>% filter(term=="lotteryvariance.SPD") %>% select("estimate")
  GER_2013_PECS_SPD_Variance_SE <- se_robust(m2.gles.cs.2013)["lotteryvariance.SPD"] #tidy(m2.gles.cs.2013) %>% filter(term=="lotteryvariance.SPD") %>% select("std.error")
  GER_2013_PECS_SPD_Mean_Estimate <- tidy(m2.gles.cs.2013) %>% filter(term=="lotterymean.SPD") %>% select("estimate")
  GER_2013_PECS_SPD_Mean_SE <- se_robust(m2.gles.cs.2013)["lotterymean.SPD"] #tidy(m2.gles.cs.2013) %>% filter(term=="lotterymean.SPD") %>% select("std.error")
  
  GER_2013_PECS_FDP_Variance_Estimate <- tidy(m3.gles.cs.2013) %>% filter(term=="lotteryvariance.FDP") %>% select("estimate")
  GER_2013_PECS_FDP_Variance_SE <- se_robust(m3.gles.cs.2013)["lotteryvariance.FDP"] #tidy(m3.gles.cs.2013) %>% filter(term=="lotteryvariance.FDP") %>% select("std.error")
  GER_2013_PECS_FDP_Mean_Estimate <- tidy(m3.gles.cs.2013) %>% filter(term=="lotterymean.FDP") %>% select("estimate")
  GER_2013_PECS_FDP_Mean_SE <- se_robust(m3.gles.cs.2013)["lotterymean.FDP"] #tidy(m3.gles.cs.2013) %>% filter(term=="lotterymean.FDP") %>% select("std.error")
  
  GER_2013_PECS_GR_Variance_Estimate <- tidy(m4.gles.cs.2013) %>% filter(term=="lotteryvariance.GRUENE") %>% select("estimate")
  GER_2013_PECS_GR_Variance_SE <- se_robust(m4.gles.cs.2013)["lotteryvariance.GRUENE"] #tidy(m4.gles.cs.2013) %>% filter(term=="lotteryvariance.GRUENE") %>% select("std.error")
  GER_2013_PECS_GR_Mean_Estimate <- tidy(m4.gles.cs.2013) %>% filter(term=="lotterymean.GRUENE") %>% select("estimate")
  GER_2013_PECS_GR_Mean_SE <- se_robust(m4.gles.cs.2013)["lotterymean.GRUENE"] #tidy(m4.gles.cs.2013) %>% filter(term=="lotterymean.GRUENE") %>% select("std.error")
  
  # Harmonize names of the IVs of interest in the models
  names(m1.gles.cs.2013$coefficients)[names(m1.gles.cs.2013$coefficients) == "lotteryvariance.UNION"] <- "Government Lottery Variance"
  names(m1.gles.cs.2013$coefficients)[names(m1.gles.cs.2013$coefficients) == "lotterymean.UNION"] <- "Government Lottery Mean"
  names(m2.gles.cs.2013$coefficients)[names(m2.gles.cs.2013$coefficients) == "lotteryvariance.SPD"] <- "Government Lottery Variance"
  names(m2.gles.cs.2013$coefficients)[names(m2.gles.cs.2013$coefficients) == "lotterymean.SPD"] <- "Government Lottery Mean"
  names(m3.gles.cs.2013$coefficients)[names(m3.gles.cs.2013$coefficients) == "lotteryvariance.FDP"] <- "Government Lottery Variance"
  names(m3.gles.cs.2013$coefficients)[names(m3.gles.cs.2013$coefficients) == "lotterymean.FDP"] <- "Government Lottery Mean"
  names(m4.gles.cs.2013$coefficients)[names(m4.gles.cs.2013$coefficients) == "lotteryvariance.GRUENE"] <- "Government Lottery Variance"
  names(m4.gles.cs.2013$coefficients)[names(m4.gles.cs.2013$coefficients) == "lotterymean.GRUENE"] <- "Government Lottery Mean"
  names(m5.gles.cs.2013$coefficients)[names(m5.gles.cs.2013$coefficients) == "lotteryvariance.LINKE"] <- "Government Lottery Variance"
  names(m5.gles.cs.2013$coefficients)[names(m5.gles.cs.2013$coefficients) == "lotterymean.LINKE"] <- "Government Lottery Mean"
  
  
  if(!robustnesscheck_pid){
    stargazer(m1.gles.cs.2013, m2.gles.cs.2013, m3.gles.cs.2013, m4.gles.cs.2013, title="Linear regressions of vote choice on perceived government lottery variance and mean (GLES Cross Section (Pre-Election) 2013).", 
              dep.var.labels=c("Union", "SPD", "FDP", "Greens", "Left"), no.space=T,
              omit.stat=c("LL","ser","f"),
              se = lapply(list(m1.gles.cs.2013, m2.gles.cs.2013, m3.gles.cs.2013, m4.gles.cs.2013), se_robust),
              p = lapply(list(m1.gles.cs.2013, m2.gles.cs.2013, m3.gles.cs.2013, m4.gles.cs.2013), p_robust),
              type = "text",
              out = "TableSM11.tex"
    )
  }

